﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace Sec_scan安全漏洞扫描系统_2._0.Services
{
    /// <summary>
    /// 网站基础信息扫描
    /// </summary>
    public class Tl_DomainScan
    {
        /// <summary>
        /// 获取网站标题
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetPageTitleAsync(string url)
        {
            try
            {
                var response = await Services_Class.Tl_CustomHttpClient_Class.Instance.GetStringAsync(url);

                // 使用正则表达式提取 <title> 标签中的内容
                var titleRegex = new Regex(@"<title>(.*?)<\/title>", RegexOptions.IgnoreCase);
                var match = titleRegex.Match(response);

                if (match.Success)
                {
                    return match.Groups[1].Value.Trim();
                }

                // 如果未找到 <title>，可以返回 "未找到标题"
                return "未找到标题";
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取标题时发生错误: {ex.Message}");
                return "错误";
            }
        }
        /// <summary>
        /// 获取网站服务信息
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async Task<string> GetWebServiceInfoAsync(string url)
        {
            try
            {
                var response = await Services_Class.Tl_CustomHttpClient_Class.Instance.GetAsync(url);
                if (response.Headers.TryGetValues("Server", out var serverValues))
                {
                    return string.Join(", ", serverValues);
                }
                return "未找到服务信息";
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取服务信息时发生错误: {ex.Message}");
                return "错误";
            }
        }
    }
}
